package com.easyboot.web.system.controller;

import cn.hutool.core.util.StrUtil;
import com.easyboot.framework.domain.DataTableDto;
import com.easyboot.framework.domain.ResultAdaptDto;
import com.easyboot.framework.core.ActionResult;
import com.easyboot.framework.core.BaseController;
import com.easyboot.framework.core.RequestHelper;
import com.easyboot.web.system.domain.SysPosition;
import com.easyboot.web.system.service.SysPositionService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author yushuo
 * @className
 * @descripton TODO
 * @date 2021/2/25 14:31
 **/
@RequestMapping("/admin/position")
@Controller
public class SysPositionController extends BaseController {

    @Autowired
    private RequestHelper requestHelper;
    @Autowired
    private SysPositionService postionService;

    /**
    *
     * @author yushuo
     * @description 列表页
     * @date 14:32 2021/2/25
     * @param []
     * @return com.easyboot.framework.web.ActionResult
    **/
    @RequiresPermissions("system:post:view")
    @GetMapping("/index")
    public ActionResult index(){
        return view();
    }

    /**
    *
     * @author yushuo
     * @description 获取职位信息列表
     * @date 14:52 2021/2/25
     * @param [dto]
     * @return com.easyboot.framework.web.ActionResult
    **/
    @RequiresPermissions("system:post:view")
    @PostMapping("/getList")
    public ActionResult getList(DataTableDto dto){
         String postName=requestHelper.getString("postName","");
         String postCode=requestHelper.getString("postCode","");

         dto=postionService.getList(dto,postName,postCode);
         return json(dto);
    }

    @RequiresPermissions(value = {"system:post:add","system:post:edit"},logical = Logical.OR)
    @GetMapping("/form")
    public ActionResult form(String id, Model model){

        model.addAttribute("id",id);
        return view();
    }

    /**
    *
     * @author yushuo
     * @description 获取职位信息
     * @date 14:51 2021/2/25
     * @param []
     * @return com.easyboot.framework.web.ActionResult
    **/
    @RequiresPermissions(value = {"system:post:add","system:post:edit"},logical = Logical.OR)
    @GetMapping("/getModel")
    public ActionResult getModel(){
        Long id=requestHelper.getInt64("id",0L);
        if(id==0L){
            return error("数据加载失败");
        }

        SysPosition postion= postionService.getModel(id);
        ResultAdaptDto ret=new ResultAdaptDto();
        ret.put("model",postion);
        return json(ret);
    }
    /**
    *
     * @author yushuo
     * @description 保存数据
     * @date 14:55 2021/2/25
     * @param [dto]
     * @return com.easyboot.framework.web.ActionResult
    **/
    @RequiresPermissions(value = {"system:post:add","system:post:edit"},logical = Logical.OR)
    @PostMapping("/save")
    public ActionResult save(SysPosition dto){

        postionService.saveModel(dto, requestHelper.getUserOwnInfo());

        return ok("职位信息保存成功");
    }

    /**
    *
     * @author yushuo
     * @description 职位删除
     * @date 15:16 2021/2/25
     * @param [ids]
     * @return com.easyboot.framework.web.ActionResult
    **/
    @RequiresPermissions("system:post:delete")
    @GetMapping("/removeAll")
    public ActionResult removeAll(String ids){

        if(StrUtil.isEmpty(ids)){
            return error("删除失败");
        }
        postionService.removeAll(ids);

        return ok("职位数据已删除");
    }
}
